﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSLib.CodeFormating.Formatters;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace LuaCodeStyler
{
	public static class Templates
	{
		public static void Load()
		{
			try
			{
				string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
				path += "\\" + Application.ProductName + "\\" + Application.ProductVersion + "\\foramttersData.dat";
				if (!File.Exists(path)) return;
				BinaryFormatter f = new BinaryFormatter();
				MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(path));
				object[] data = f.Deserialize(stream) as object[];
				FormatConfiguration[] sett = data[1] as FormatConfiguration[];
				currentTemplateIndex = (int)data[0];
				SettingsList.Clear();
				SettingsList.AddRange(sett);
			}
			catch (Exception ex)
			{
				MessageBox.Show("Cannot save template list:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		public static void Save()
		{
			try
			{
				string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
				path += "\\" + Application.ProductName + "\\" + Application.ProductVersion + "\\foramttersData.dat";
				if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path));
				BinaryFormatter f = new BinaryFormatter();
				MemoryStream data = new MemoryStream();
				f.Serialize(data, new object[] { currentTemplateIndex, Templates.SettingsList.ToArray() });
				System.IO.File.WriteAllBytes(path, data.ToArray());
			}
			catch (Exception ex)
			{
				MessageBox.Show("Cannot save template list:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		public static void AddFormat(FormatConfiguration settings)
		{
			SettingsList.Add(settings);
			FireFormatListChanged();
		}

		public static void RemoveFormat(int index)
		{
			SettingsList.RemoveAt(index);
			FireFormatListChanged();
		}

		public static void ReplaceFormat(int index, FormatConfiguration settings)
		{
			SettingsList[index] = settings.Clone();
			FireFormatListChanged();
		}

		public static FormatConfiguration GetFormat(int index)
		{
			FormatConfiguration result = null;
			if (SettingsList.Count > 0)
			{
				if (index < 0 || index >= SettingsList.Count) return null;
				result = SettingsList[index];
			}
			return result;
		}

		private static void FireFormatListChanged()
		{
			if (FormatListChanged != null) FormatListChanged(null, new EventArgs());
		}

		private static void FireFormatActiveIndexChanged()
		{
			if (FormatActiveIndexChanged != null) FormatActiveIndexChanged(null, new EventArgs());
		}

		public static int ActiveFormatIndex
		{
			get 
			{
				if (SettingsList.Count > 0)
				{
					if (currentTemplateIndex < 0) currentTemplateIndex = 0;
					if (currentTemplateIndex >= SettingsList.Count) currentTemplateIndex = SettingsList.Count - 1;
					return currentTemplateIndex;
				}
				return -1;
			}
			set 
			{
				if (value != currentTemplateIndex)
				{
					currentTemplateIndex = value;
					FireFormatActiveIndexChanged();
				}
			}
		}

		public static event EventHandler FormatListChanged;
		public static event EventHandler FormatActiveIndexChanged;

		public static readonly List<FormatConfiguration> SettingsList = new List<FormatConfiguration>();
		private static int currentTemplateIndex = -1;
	}
}
